package org.duang.db.sql.parser;

import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.duang.db.annotation.Vo;
import org.duang.db.annotation.VoColl;
import org.duang.db.sql.parser.decode.SqlDecoder;
import org.duang.db.sql.parser.decode.SqlPropertyDecoder;
import org.duang.kit.ClassKit;
import org.duang.kit.HostKit;
import org.duang.kit.ToolsKit;

public class SqlDecodeConvetor {
	public static <T> T convetor(Class<T> clazz, Map<String, Object> map) {
		if(ToolsKit.isEmpty(map)) return null;		
		Field[] fields = ClassKit.getFields(clazz);
		if(ToolsKit.isEmpty(fields)) return null;
		T obj = ClassKit.createConst(clazz);
		for (int i = 0; i < fields.length; i++) {
			SqlDecoder decoder = parser(map, fields[i]);
			if(null != decoder && null != decoder.getFieldValue()){
				decoder.setObj(obj);
				HostKit.addHostSite(obj, fields[i], decoder.getFieldValue());//添加OSS域名
			}
		}
		return obj;
	}
	
	private static SqlDecoder parser(Map<String, Object> map,  Field field) {
		SqlDecoder decoder = null;
		if(!field.isAnnotationPresent(Vo.class) || !field.isAnnotationPresent(VoColl.class)){		
			decoder = new SqlPropertyDecoder(map, field);
		}
		return decoder;
	}
}
